import { _decorator, Component, Node, Label } from 'cc';
import { BasicUI } from '../../Init/Basic/BasicUI';
import { GlobalEnum } from '../../Init/Config/GlobalEnum';
import { GlobalTmpData } from '../../Init/Config/GlobalTmpData';
import EventManager from '../../Init/Managers/EventManager';
import { EventTypes } from '../../Init/Managers/EventTypes';
import { StorageSystem } from '../../Init/SystemStorage/StorageSystem';
import { UIEnum } from '../../Init/SystemUI/UIEnum';
import { UISystem } from '../../Init/SystemUI/UISystem';
import { GlobalConfig } from '../../Init/Config/GlobalConfig';

import { v3 } from 'cc';

const { ccclass, property } = _decorator;
// 关卡信息
@ccclass('LevelInfoUI')
export class LevelInfoUI extends BasicUI {

    protected onLoad(): void {
        super.onLoad();
        this.initTest();
    }

    protected onEnable(): void {
        this.enableTest();
    }
    protected onDisable(): void {
    }

    public onEvents() {
 
    }

    public show(d?) {
        super.show(d);

        this.setLvInfo();

        // UISystem.hideUI(UIEnum.PlayerAssetsUI);
        // 结束Loading动画
        EventManager.emit(EventTypes.UIEvents.ShowLoadingExitAnim);

        UISystem.showUI(UIEnum.PlayerAssetsUI);
        UISystem.showUI(UIEnum.LevelController);

        console.log('#time4 ', new Date().getTime() - GlobalTmpData.Game.gameLaunchTime);

    }

    protected update(dt) {
    }
    public hide(d?: any): void {
        super.hide(d);
        UISystem.hideUI(UIEnum.LevelController);
    }

    // #region --------信息--------

    @property(Label)
    protected lvLabel: Label = null;
 
    private setLvInfo() {
        const _data = StorageSystem.getData();
        this.lvLabel.string = _data.levelAssets.curLv + '';
      
        let isPop = false;
        // this.backTip.node.active = false;


        if (isPop) {
            StorageSystem.saveData();
        }
    }

    // #endregion

    // #region ----------事件


    // #endregion

    //#region --- UI按钮

    onBackHome() {
        if (GlobalTmpData.Game.isGameOver) return
        EventManager.emit(EventTypes.GameEvents.GamePause);
        // 退出 Log
        EventManager.emit(EventTypes.SDKEvents.ExitStage, GlobalEnum.ExitLvState.Out);

        UISystem.showUI(UIEnum.LoadingUI, {
            enterCb: () => {

                this.emit(EventTypes.GameEvents.BackHome, true);
            }
        })
    }

    //#endregion

    //#region -----测试---------
    @property(Node)
    protected testNode: Node = null;

    private _timeScale = 1.0;

    private initTest() {
        this.testNode.active = GlobalConfig.isDebug;
    }
    private enableTest() {
        this._timeScale = 1.0;
    }

    protected onSpdUpClick() {
        this._timeScale += 0.5;
        this.emit(EventTypes.GameEvents.SetGameTimeScale, this._timeScale);
    }
    protected onSpdCutClick() {
        this._timeScale -= 0.5;
        this._timeScale = this._timeScale < 0 ? 0 : this._timeScale;
        this.emit(EventTypes.GameEvents.SetGameTimeScale, this._timeScale);
    }
    protected testWin() {
        this.emit(EventTypes.GameEvents.GameOver, true);
    }

    protected testLose() {
        this.emit(EventTypes.GameEvents.GameOver, false);        
    }

    protected testPause() {
        this.emit(EventTypes.GameEvents.GamePause);
    }

    protected testResume() {
        this.emit(EventTypes.GameEvents.GameResume);
    }

    protected testBackHome() {
        UISystem.hideUI(UIEnum.LevelInfoUI);
        this.emit(EventTypes.GameEvents.BackHome, true);
    }

    // #endregion ------------


}

const tmpP3_0 = v3();
const tmpP3_1 = v3();
const tmpP3_2 = v3();
